<?php
//This code was created by Me. And by "Me", I mean Nathan Rice.
//Referring to myself in the 3rd person would be douche-ish, no?

$wp_theme_name = "Proximity News";
$wp_theme_shortname = "px";

// Pull options from database, to use them in the theme
$wp_theme_options = get_option($wp_theme_shortname.'-options');
// Be sure to globalize the variable above in the theme files you'd like to use it in.
global $wp_theme_options; //wrapped in php tags

// Add the admin
function wp_theme_admin() {

// Use Globals
global $wp_theme_name;
global $wp_theme_shortname;

add_theme_page($wp_theme_name." Theme Options", $wp_theme_name." Theme Options", 'edit_themes', basename(__FILE__), 'wp_theme_adminpage_print');
}

// Process POST vars
function wp_theme_processforms() {
	
// Use Globals
global $wp_theme_name;
global $wp_theme_shortname;
global $wp_theme_options;
	
	//If User chose to save
	if ( 'save' == $_REQUEST['action'] ) {
	
	// Define and Sanitize (if necessary) before store
	$wp_theme_options['feedburner_url'] = $_POST['feedburner_url'];
	$wp_theme_options['exclude_pages'] = $_POST['exclude_pages'];
	$wp_theme_options['feature_cat'] = intval($_POST['feature_cat']);
	$wp_theme_options['post_by_cat'] = $_POST['post_by_cat'];
	$wp_theme_options['post_per_cat'] = intval($_POST['post_per_cat']);
	$wp_theme_options['other_title'] = $_POST['other_title'];
	$wp_theme_options['other_posts'] = intval($_POST['other_posts']);
	$wp_theme_options['feature_video'] = $_POST['feature_video'];
	$wp_theme_options['adsense_468x60'] = $_POST['adsense_468x60'];
	$wp_theme_options['adsense_250x250'] = $_POST['adsense_250x250'];
	$wp_theme_options['tracking'] = $_POST['tracking'];
	$wp_theme_options['tracking_pos'] = $_POST['tracking_pos'];
	$wp_theme_options['tag_as_keyword'] = $_POST['tag_as_keyword'];
	$wp_theme_options['cat_index'] = $_POST['cat_index'];
        $wp_theme_options['full_archive'] = $_POST['full_archive'];
        $wp_theme_options['full_timeline'] = $_POST['full_timeline'];
	
	// Store
	update_option($wp_theme_shortname.'-options',$wp_theme_options);
	echo '
	<div class="updated fade" id="message"><p>Theme Options <strong>SAVED</strong>.</p></div>
	';
	}
	
	//If User chose to reset
	else if( 'reset' == $_REQUEST['action'] ) {
	// Delete/Reset
	delete_option($wp_theme_shortname.'-options');
	echo '
	<div class="updated fade" id="message"><p>Theme Options <strong>RESET</strong>.</p></div>
	';
	}
}

// Print admin page
function wp_theme_adminpage_print() {
	
if (isset($_POST['wp_theme_options_submit']) && ($_POST['wp_theme_options_submit'] == 1) ) wp_theme_processforms();

// Set Globals
global $wp_theme_name;
global $wp_theme_shortname;
global $wp_theme_options;
global $wpdb;

// Pull options from database
$wp_theme_options = get_option($wp_theme_shortname.'-options');
// Set variables
extract((array)$wp_theme_options);

wp_theme_adminpage_js(); // custom javascript
wp_theme_options_adminpage_css(); // custom css
?>
<div style="display:none;">
<!--<?php print_r($wp_theme_options); ?>-->
</div>
    <div class="wrap">
    <h2><?php echo $wp_theme_name; ?> Theme Settings</h2>
    <form method="post" action="" id="wp_theme_options">
	<?php wp_nonce_field('wp_theme_options_submit'); ?>
	<table class="form-table"><tbody>
	<input type="hidden" name="wp_theme_options_submit" value="1"/>
<?php /*    
	<tr><th scope="row">Feedburner URL</th>
	<td>If you use a site like <a href="http://feedburner.com/">Feedburner</a> to handle your feeds, please enter your feed URL here (including http://):<br />
	<input type="text" value="<?php echo $feedburner_url; ?>" name="feedburner_url" id="feedburner_url" class="textbox"> (leave blank for none)
	</td></tr>
*/ ?>	
	<tr><th scope="row">Exclude Pages from Navigation</th>
	<td>Select any Pages you would like excluded from the horizontal navigation bar, including the "<strong>Home</strong>" link.<br />
	You may select more than one page by ctrl+clicking (command+click on the Mac) multiple Pages:<br />
	<select multiple name="exclude_pages[]" id="exclude_pages" class="multiselect" size="100">
	<option value="home"<?php if ($exclude_pages && in_array('home', $exclude_pages)) { echo ' selected="selected"'; } ?>>Home</option>
	<?php $pages = get_pages();
	foreach ($pages as $page) {
	if ($exclude_pages && in_array($page->ID, $exclude_pages)) { $selected = ' selected="selected"'; } else { $selected = ''; }
	$opt = '<option value="' . $page->ID . '"' . $selected . '>' . $page->post_title . '</option>';
	echo $opt; } ?>
	</select>
	</td></tr>
	
	<tr><th scope="row">Feature Category</th>
	<td>Please select the category you would like to use to populate the "Feature" section on the front page:<br />
	<select name="feature_cat" id="feature_cat" class="dropdown">
	<?php $categories = get_categories('hide_empty=0');
	foreach ($categories as $cat) {
	if ($feature_cat == $cat->cat_ID) { $selected = ' selected="selected"'; } else { $selected = ''; }
	$opt = '<option value="' . $cat->cat_ID . '"' . $selected . '>' . $cat->cat_name . '</option>';
	echo $opt; } ?>
	</select>
	</td></tr>

	<tr><th scope="row">Post By Category</th>
	<td>Select the categories that you would like to have featured on the front page:<br />
	<select multiple name="cat_opt[]" id="cat_opt" class="multiselect" size="100">
	<?php $categories = get_categories('hide_empty=0');
	foreach ((array)$categories as $cat) {
	if (!in_array($cat->cat_ID, (array)$post_by_cat)) {
	$opt = '<option value="' . $cat->cat_ID . '"' . $selected . '>' . $cat->cat_name . '</option>';
	echo $opt; } } ?>
	</select>
	<select multiple name="post_by_cat[]" id="post_by_cat" class="multiselect" size="100">
	<?php foreach ((array)$post_by_cat as $cat_id) {
	$opt = '<option value="' . $cat_id . '"' . '>' . prox_cat_name($cat_id) . '</option>';
	echo $opt; } ?>
	</select>
	<div style="width: 420px;">
	<button type="button" id="add" style="float:left;">Add &raquo;</button>
	<button type="button" id="remove" style="float:right;">&laquo; Remove</button>
	</div><br /><br />
	<input style="width: 20px;" type="text" value="<?php if($post_per_cat){echo $post_per_cat;} else {echo '1';} ?>" name="post_per_cat" id="post_per_cat" class="textbox">
	How many posts do you want to display per category? (default = 1)
	</td></tr>
	
	<tr><th scope="row">Other Headlines Section</th>
	<td>What would you like the title for the "Other Headlines" section to be:<br />
	<input type="text" value="<?php if($other_title){echo stripslashes($other_title);} else {echo 'Other Headlines';} ?>" name="other_title" id="other_title" class="textbox"><br /><br />
	<input style="width: 20px;" type="text" value="<?php if($other_posts){echo $other_posts;} else {echo '4';} ?>" name="other_posts" id="other_posts" class="textbox">
	How many posts would you like to display in the "Other Headlines" section? (default = 4)
	</td></tr>
        
        <tr><th scope="row">Full or Shortened Posts Section</th>
	<td>Check for Full Posts shown in the pages below:<br />
	<input type="checkbox" value="1" name="full_archive" id="full_archive" <?php checked('1', $full_archive); ?> > All Category and Tag Lists<br />
	<input type="checkbox" value="1" name="full_timeline" id="full_timeline" <?php checked('1', $full_timeline); ?> > Timeline Page<br />
<?php /*	
	<tr><th scope="row">Feature Video Code</th>
	<td>Paste the embed code for the video (like <a href="http://www.youtube.com/">YouTube</a> or <a href="http://vimeo.com">Vimeo</a>) you would like featured on the front page:<br />
	<textarea name="feature_video" id="feature_video" class="textarea" rows="3" cols="50"><?php echo stripslashes($feature_video); ?></textarea>
	</td></tr>
	
	<tr><th scope="row">Adsense 468x60 Block</th>
	<td>Paste the adsense code to be used under the feature story on the front page:<br />
	(leave blank for none)<br />
	<textarea name="adsense_468x60" id="adsense_468x60" class="textarea" rows="3" cols="50"><?php echo stripslashes($adsense_468x60); ?></textarea>
	</td></tr>
	
	<tr><th scope="row">Adsense 250x250 Block</th>
	<td>Paste the adsense code to be used under the tabber section in the front page sidebar:<br />
	(leave blank for none)<br />
	<textarea name="adsense_250x250" id="adsense_250x250" class="textarea" rows="3" cols="50"><?php echo stripslashes($adsense_250x250); ?></textarea>
	</td></tr>
	
	<tr><th scope="row">Tracking Code</th>
	<td>If you use a tracking service like <a href="http://google.com/analytics">Google Analytics</a>, paste the tracking code in the box below:<br />
	(leave blank for none)<br />
	<textarea name="tracking" id="tracking" class="textarea" rows="3" cols="50"><?php echo stripslashes($tracking); ?></textarea><br />
	Does your tracking service go in the header or footer of the code?<br />
	<input type="radio" name="tracking_pos" value="header" <?php if($tracking_pos == 'header') echo 'checked'; ?>> Header<br />
	<input type="radio" name="tracking_pos" value="footer" <?php if($tracking_pos == 'footer') echo 'checked'; ?>> Footer
	</td></tr>
	
	<tr><th scope="row">Search Engine Optimization</th>
	<td>Would You like to use post tags as <a href="http://en.wikipedia.org/wiki/Meta_element#The_keywords_attribute" target="_blank">META keywords</a> on single posts? (recommended)<br />
	<input type="radio" name="tag_as_keyword" value="yes" <?php if($tag_as_keyword == 'yes') echo 'checked'; ?>> Yes<br />
	<input type="radio" name="tag_as_keyword" value="no" <?php if($tag_as_keyword == 'no') echo 'checked'; ?>> No<br />
	<strong>NOTE:</strong> By default, this theme uses either <a href="http://codex.wordpress.org/Template_Tags/the_excerpt_rss" target="_blank">the excerpt</a> on single posts or pages,<br />
	or the blog <a href="<?php bloginfo('wpurl'); ?>/wp-admin/options-general.php" target="_blank">tagline</a> on all other pages (home, archives, etc.) as the <a href="http://en.wikipedia.org/wiki/Meta_element#The_description_attribute" target="_blank">META description</a>.<br /><br />
	
	Would you like your category archives to be indexed by search engines? (<strong>not</strong> recommended)<br />
	<input type="radio" name="cat_index" value="yes" <?php if($cat_index == 'yes') echo 'checked'; ?>> Yes<br />
	<input type="radio" name="cat_index" value="no" <?php if($cat_index == 'no') echo 'checked'; ?>> No<br />
	<strong>NOTE:</strong> No date based archives or search results will be indexed by Search Engines.
	</td></tr>
*/ ?>		
	</tbody></table>
	
	<p class="submit">
	<input name="save" type="submit" value="Save Options &raquo;" />	
	<input type="hidden" name="action" value="save" />
	</p>
	</form>
<?php /*
	<form method="post">
	<p class="submit">
	<input name="reset" type="submit" value="Reset Options &raquo;" />
	<input type="hidden" name="wp_theme_options_submit" value="1"/>
	<input type="hidden" name="action" value="reset" />
	</p>
	</form>
*/ ?>
	</form>
	</div>
<?php }
function wp_theme_options_adminpage_css() { //if you would like to add CSS to the admin page
	echo <<<CSS
	<style type="text/css">
		#wp_theme_options .dropdown {
			width: 210px;
		}
		#wp_theme_options .multiselect {
			height: 100px;
			width: 210px;
		}
		#wp_theme_options .textbox {
			width: 400px;
		}
		#wp_theme_options .textarea {
			width: 400px;
		}
	</style>
	
	
CSS;
}
function wp_theme_adminpage_js() { // if you would like to add javascript to the admin page
	echo <<<JS
	<script type="text/javascript">
	jQuery().ready(function() {  
		jQuery('#add').click(function() {  
			return !jQuery('#cat_opt option:selected').remove().appendTo('#post_by_cat');  
		});  
		jQuery('#remove').click(function() {  
			return !jQuery('#post_by_cat option:selected').remove().appendTo('#cat_opt');  
		});
		jQuery('form').submit(function() {  
			jQuery('#post_by_cat option').each(function(i) {  
				jQuery(this).attr("selected", "selected");  
			});  
		});
	});  
	</script>
JS;

}
add_action('admin_menu', 'wp_theme_admin'); 
?>